package photostock.model.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;

import photostock.control.ItemCache;
import photostock.model.Album;
import photostock.model.ItemFactory;
import photostock.model.Photo;
import photostock.model.User;

public class PhotoTest {

	private Photo photo;
	private Photo photo2;
	
	@Before
	public void photo() {
		
		User user = new User("nico");
		assertTrue (user.exists() && user.isDirectory() && !user.create());
		assertTrue(user.getLogin().equals("nico"));
		assertTrue(user.login("atreides"));
		assertTrue(user.initChildrenItems());
		Album album = (Album)user.getChildrenItems().elementAt(0);
		assertTrue(album.getChildrenItems().size() == 0);
		assertTrue(album.initChildrenItems());
		assertTrue(album.getChildrenItems().size() == 14);
		photo = (Photo)album.getChildrenItems().elementAt(0);
		photo.initChildrenItems();
		photo2 = (Photo)photo.getChildrenItems().elementAt(0);
		assertTrue("non plus ...", ItemFactory.isCached(photo));
		assertTrue("non plus ...", ItemFactory.isCached(photo2));
		assertEquals(photo.getWebPath(), photo.getParentItem().getWebPath() + "/" + photo.getName());
		assertEquals(photo2.getWebPath(), photo.getParentItem().getWebPath() + "/" + Photo.ICON + photo.getName());
		photo2 = (Photo)photo.getChildrenItems().elementAt(0);
		
	}
	

	@Test
	public void photoPartage() {
		photo.partager();
		assertTrue("Is partagee : " + photo.isPartagee(),  photo.isPartagee());
	}
	
	@Test
	public void photoVisite() {
		int nbVisites = photo.getNbVisites();
		photo.visiter();
		assertTrue(photo.getNbVisites() == nbVisites + 1);
	}

	@Test
	public void photoVote() {
		int nbVotes = photo.getNbVotes();
		photo.voter();
		assertTrue(photo.getNbVotes() == nbVotes + 1);
	}

	@Test
	public void photoValide() {
		photo.valider();
		assertTrue(photo.isValidee());
	}

	@Test
	public void photoToMin() {
		assertTrue("photo.toMin() : " + photo.toMin().getName(), photo.toMin().getName().startsWith("tn_"));
	}

	@Test
	public void photoToMax() {
		assertTrue("photo.toMax() : " + photo.toMax().getName(), photo.toMax().getName().startsWith("mx_"));
		assertTrue("photo2.toMax() : " + photo2.toMax().getName(), photo2.toMax().getName().startsWith("mx_"));
	}

	@Test
	public void photoSize() {
		assertTrue("photo.getWidth() : " + photo.toMin().getWidth(), photo.toMin().getWidth() == Photo.ICONSIZE || photo.toMin().getHeight() == Photo.ICONSIZE);
		assertTrue("photo2.getWidth() : " + photo2.getWidth(), photo2.getWidth() == Photo.ICONSIZE || ((Photo)photo.toMin()).getHeight() == Photo.ICONSIZE);
	}
	
	@Test
	public void photoCache() {
		assertTrue(ItemCache.getInstance().isInCache(photo.getName()+photo.getId()));	
		assertTrue(ItemCache.getInstance().isInCache(photo2.getName()+photo2.getId()));	
	}
	
	@Test
	public void photoDel() {
		System.out.println(photo2.getParent());
		System.out.println(photo2.getParent());
		photo2.delete();
		assertFalse(photo2.exists());
		assertFalse("" + ItemCache.getInstance().isInCache(photo2.getName()), ItemCache.getInstance().isInCache(photo2.getName()));	
		
	}
	
}
